<template>
  <div class="pghome">
    <div class="menu_container" v-for="father in menuData" :key="father.id">
      <div class="menu_title">{{ father.title }}</div>
      <div style="display: flex">
        <div
          class="menu_item"
          v-for="child in father.children"
          :key="child.id"
          @click="navigateTo(child)"
        >
          <span> {{ child.name }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqHomeMenu } from "@/services/home";

export default {
  name: "PgHome",
  data() {
    return {
      menuData: [],
    };
  },
  methods: {
    async getMenu() {
      const res = await reqHomeMenu();
      // console.log(res);
      if (res?.respCode == 200) {
        let add = [];
        res.respData.forEach((item) => {
          item.children.forEach((err) => {
            add.push(err);
          });
        });
        localStorage.setItem("content", JSON.stringify(add));
        this.menuData = res.respData;
      }
    },
    navigateTo(obj) {
      if (!obj.url) return this.$message.error("当前菜单没有URL")

      //type : "1"是路由 "2"是iframe
      if (obj.type === "1") {
        this.$router.push(obj.url);
      }else if (obj.type === "2") {
        this.$router.push(`/PgIframe/${obj.id}`);
      }
    },
  },
  mounted() {
    this.getMenu();
  },
};
</script>

<style scoped lang="scss">
.pghome {
  width: 100%;
  height: 100%;

  .menu_container {
    padding: 10px;

    .menu_title {
      background-color: #f5f5f5;
      margin-bottom: 20px;
      height: 30px;
      line-height: 30px;
    }

    .menu_item {
      height: 96px;
      margin-right: 10px;
      width: 7em;
      border-radius: 8px;
      background-color: #496fff;
      line-height: 96px;
      text-align: center;
      color: #ffffff;
      cursor: pointer;
      user-select: none;
    }
  }
}
</style>